<?php

namespace Biz\model\activity;

use Doraemon\pockets\datebase\BaseDbModel;
use Hhzbeads\Img\ImgUpload;
use Doraemon\tools\ImgUrl as toolImgUrl;

class PopupList extends BaseDbModel
{
    const _CONFIG_FILE_ = 'db_shop';

    const _CONFIG_SELECT_ = 'db_activity';

    const _DATABASE_ = 'shop_activity';

    const _TABLE_NAME = 'popup_list';


    //出现位置
    const COUPON_SHOW_TYPE_LOCATION_APP = 1;//商城首页优惠券弹窗

    //弹窗类型
    const COUPON_SHOW_RULE_ALL = 1;//所有人可见
    const COUPON_SHOW_RULE_BY_REGISTER_TIME = 2;//N小时注册用户可见

    public static $typeLocationDesc = [
        self::COUPON_SHOW_TYPE_LOCATION_APP => '商城首页优惠券弹窗'
    ];

    public static $showRuleDesc = [
        self::COUPON_SHOW_RULE_ALL => '所有人可见',
        self::COUPON_SHOW_RULE_BY_REGISTER_TIME => 'N小时内注册用户可见'
    ];

    public static function create($data)
    {
        $data['addtime'] = date('Y-m-d H:i:s');
        $ret = self::insert($data);
        if ($ret->rowCount()) {
            return self::id();
        }
        return false;
    }

    public static function upData($where, $data)
    {
        $ret = self::update($data, $where);
        if ($ret->errorCode() !== '00000') {
            return false;
        }
        return true;
    }

    public static function getList($fields = '*', $where = [], $limit = [])
    {
        if ($limit) {
            $where['LIMIT'] = $limit;
        }

        $where['ORDER'] = ['addtime' => 'DESC'];
        $result = self::select($fields, $where);
        if (empty($result)) {
            return [];
        }
        $list = [];
        foreach ($result as $info) {
            self::format($info);
            $list[] = $info;
        }
        return $list;
    }

    public static function getPopupInfo($id, $fields = '*')
    {
        $info = self::get($fields, ['id' => $id]);

        if (empty($info)) {
            return [];
        }
        self::format($info);
        return $info;
    }

    private static function format(&$info)
    {
        foreach ($info as $key => $value) {
            switch ($key) {
                case 'show_type':
                    $info['show_type_desc'] = self::$typeLocationDesc[$value];
                    break;
                case 'show_rule':
                    $info['show_rule_desc'] = self::$showRuleDesc[$value];
                    break;
                case 'coupon_id':
                    $info['coupon_ids'] = [];
                    if (explode(',', $info['coupon_id']) !== false) {
                        $info['coupon_ids'] = explode(',', $info['coupon_id']);
                    }
                    break;
                case 'banner':
                    $info['banner_img_ori'] = toolImgUrl::getUrl(toolImgUrl::APPTYPE_BIZ, $info['banner'], toolImgUrl::IMAGE_SIZE_ORI_NPHONE);
                    $info['banner_img_sq_thumb'] = toolImgUrl::getUrl(toolImgUrl::APPTYPE_BIZ, $info['banner'], toolImgUrl::IMAGE_SIZE_SQ_THUMB);
                    break;
            }
        }
    }
}